//
//  UserCenter.m
//  Pitch
//
//  Created by zhujinhui on 14-9-3.
//  Copyright (c) 2014年 zhujinhui. All rights reserved.
//

#import "UserCenter.h"

@implementation UserCenter

static UserCenter *userCenter = nil;


+(UserCenter *) shareInstance{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        userCenter = [[UserCenter alloc]init];
    });

    return userCenter;
}

-(id)init{
    if (self = [super init]) {
        [self initInfo];
    }
    return self;
}


-(void)initInfo{
    NSData *userData = [[NSUserDefaults standardUserDefaults]objectForKey:USERDEFAULTS_USER];
    User *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    @try {
        if (user && user.userId) {
            _user = user;
            _hasLogined = YES;
        }else{
            _user = nil;
            _hasLogined = NO;
        }
    }
    @catch (NSException *exception) {
        _user = nil;
        _hasLogined = NO;
        user = nil;
    }
    
}


-(void)setUser:(User *)user{
    _user = user;
    if (user) {
        _hasLogined = YES;
        NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:_user];//archiver
        [[NSUserDefaults standardUserDefaults]setObject:userData forKey:USERDEFAULTS_USER];
    }
    
}



@end

